﻿using LoveEatRabbit.API.Read.Applications.Commands.Roles;
using LoveEatRabbit.API.Read.DTOS;
using LoveEatRabbit.Domain.RBAC;
using LoveEatRabbit.ErrorCode;
using LoveEatRabbit.Infrastructure.Interfaces;
using MediatR;

namespace LoveEatRabbit.API.Read.Applications.CommandHandlers.Roles
{
    public class RoleBackQueryCommandHandler : IRequestHandler<RoleBackQueryCommand, APIResult<List<RolePerDTO>>>
    {
        private readonly IBaseRespository<RoleModel> roleRep;
        private readonly IBaseRespository<RolePermissionModel> roleperRep;

        public RoleBackQueryCommandHandler(IBaseRespository<RoleModel> roleRep,
            IBaseRespository<RolePermissionModel> roleperRep)
        {
            this.roleRep = roleRep;
            this.roleperRep = roleperRep;
        }

        public Task<APIResult<List<RolePerDTO>>> Handle(RoleBackQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<RolePerDTO>> result=new APIResult<List<RolePerDTO>>();
            result.Code=APIResultCode.查询成功;
            result.Message=APIResultCode.查询成功.ToString();

            var from = from a in roleRep.GetAll()
                       join b in roleperRep.GetAll() on a.RoleId equals b.RoleId
                       where a.RoleId == request.RoleId
                       select new RolePerDTO
                       {
                           RoleName = a.RoleName,
                           PermissionId=b.PermissionId,
                       };
            result.Data=from.ToList();


            return Task.FromResult(result);
        }
    }
}
